package com.sky.contriller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping()
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址:{}",addressBook);

        addressBookService.save(addressBook);

        return Result.success();
    }

    /**
     * 查看用户所有地址
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> addressBook = addressBookService.list();

        return Result.success(addressBook);
    }

    /**
     * 查看默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook = addressBookService.getDefault();

        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);

        return Result.success();
    }

    /**
     * 根据id查询数据 ， 数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("查询id为：{}的数据",id);

        AddressBook addressBook = addressBookService.getById(id);

        return Result.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);

        return Result.success();
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteById(Long id){
        log.info("要删除的地址为：{}",id);

        addressBookService.deleteById(id);

        return Result.success();
    }

}
